/****************************************************************************
 * 2025.5 阿桑的笔记本
 ****************************************************************************/

using System;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
using QFramework;

namespace QFramework.Example
{
    public partial class PlayerHpRoot : UIElement, IController,ICanReadModels
	{
        public List<GameObject>HPList = new List<GameObject>();

		private void Awake()
		{
            SetStartHp();
            ReadModels();

        }

		protected override void OnBeforeDestroy()
		{
		}

        public IArchitecture GetArchitecture()
        {
			return Global.Interface;
        }

        public void ReadModels()
        {
            ///角色生命值监听
            DataManager.Instance.playerModel.PlayerHp.Register(value =>
            {

                if (value> HPList.Count)
                {
                    var hpCount = value - HPList.Count;
                    for (int i = 0; i < hpCount; i++)
                    {
                        PlayerHp.InstantiateWithParent(this).Self(self =>
                        {
                            var selfChage = self;
                            HPList.Add(selfChage.gameObject);
                            
                        }).Show();
                    }

                }
                if (value<HPList.Count)
                {
                    var hpCount =  HPList.Count-value;
                    Destroy(HPList[hpCount]);
                    HPList.RemoveAt(hpCount);
                }

            }).UnRegisterWhenGameObjectDestroyed(gameObject);

           
        }

        void SetStartHp()
        {
            for (int i = 0; i < DataManager.Instance.playerModel.PlayerMaxHp.Value; i++) 
            {
                PlayerHp.InstantiateWithParent(this).Self(self =>
                {
                    var selfChage = self;
                    HPList.Add(selfChage.gameObject);
                }).Show();
                
            }

            Debug.Log("集合长度为：" + HPList.Count);
        }
    }
}